import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Delete76 {
    import java.io.*;
import java.util.*;
    public class Main {
        public static void main(String[] args) throws IOException {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输⼊要扫描的根⽬录（绝对路径 OR 相对路径): ");
            String rootDirPath = scanner.next();
            File rootDir = new File(rootDirPath);
            if (!rootDir.isDirectory()) {
                System.out.println("您输⼊的根⽬录不存在或者不是⽬录，退出");
                return;
            }
            System.out.print("请输⼊要找出的⽂件名中的字符: ");
            String token = scanner.next();
            List<File> result = new ArrayList<>();
// 因为⽂件系统是树形结构，所以我们使⽤深度优先遍历（递归）完成遍历
            scanDir(rootDir, token, result);
            System.out.println("共找到了符合条件的⽂件 " + result.size() + " 个，它们分别
            for (File file : result) {
                System.out.println(file.getCanonicalPath() + " 请问您是否要删除该⽂件
                        String in = scanner.next();
                if (in.toLowerCase().equals("y")) {
                    file.delete();
                }
            }
        }
        private static void scanDir(File rootDir, String token, List<File> result) {
            File[] files = rootDir.listFiles();
            if (files == null || files.length == 0) {
                return;
            }
            for (File file : files) {
                if (file.isDirectory()) {
                    scanDir(file, token, result);
                } else {
                    if (file.getName().contains(token)) {
                        result.add(file.getAbsoluteFile());
                    }
                }
            }
        }
    }
}
